package com.ithinkstudio.jeez.config;

import com.jagregory.shiro.freemarker.ShiroTags;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.IOException;

/**
 * @description: freemarker配置
 * @author: fengzp
 * @create: 2020/04/21
 */
@Configuration
@Slf4j
public class FreemarkerConfig {

    @Resource
    private FreeMarkerConfigurer freeMarkerConfigurer;

    /**
     * shiro freemarker标签
     * @return
     */
    @Bean
    public ShiroTags shiroTags() {
        return new ShiroTags();
    }

    @PostConstruct
    public void init() throws IOException, TemplateException {
        freemarker.template.Configuration configuration = freeMarkerConfigurer.createConfiguration();
        log.debug("开始注入freemarker全局变量");
        configuration.setDefaultEncoding("UTF-8");
        //这里可以添加其他共享变量 比如sso登录地址
        configuration.setSharedVariable("shiro", shiroTags());

        //添加freemarker自定义标签
        //configuration.setSharedVariable("dict", dictDirective);
        //configuration.setSharedVariable("radio", dictRadioDirective);
        //configuration.setSharedVariable("select", dictSelectDirective);

        freeMarkerConfigurer.setConfiguration(configuration);
        log.debug("freemarker自定义标签配置完成!");
    }
}
